package com.fht.spring_boot_study.config;

import com.fht.spring_boot_study.interceptor.ViewMappingInterceptor;
import com.fht.spring_boot_study.model.common.entity.ResourceConfigBean;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@AutoConfigureAfter({WebMvcAutoConfiguration.class})
public class WebMvcConfig implements WebMvcConfigurer {
    @Value("${server.http.port}")
    private  int httpPort;
    @Autowired
    private ResourceConfigBean resourceConfigBean;
    @Autowired
    private ViewMappingInterceptor viewMappingInterceptor;
    //新建连接器
    @Bean
    public Connector connector(){
        Connector connector=new Connector();
        connector.setScheme("http");
        connector.setPort(httpPort);
        return connector;
    }

    //再tomcat添加新建的连接器，使tomcat支持http默认端口
    @Bean
    public ServletWebServerFactory servletWebServerFactory(){
        TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(connector());
        return tomcat;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().startsWith("win")){
            registry.addResourceHandler(resourceConfigBean.getResourcePathPattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX+resourceConfigBean.getLocalPathForWindow());
        }else {
            registry.addResourceHandler(resourceConfigBean.getResourcePathPattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX+resourceConfigBean.getLocalPathForLinux());
        }

    }
    //    @Bean
//    public ServletListenerRegistrationBean<EventListener> servletListenerRegistrationBean() {
//    	ServletListenerRegistrationBean<EventListener> bean = new ServletListenerRegistrationBean<EventListener>();
//    	bean.setListener(new LogListener());
//    	return bean;
//    }

//    @Bean
//    public FilterRegistrationBean filterRegistrationBean() {
//        FilterRegistrationBean bean = new FilterRegistrationBean();
//        bean.setFilter(new ParagramFilter());
//        return bean;
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(viewMappingInterceptor).addPathPatterns("/**");
    }
}
